//
//  YHGameLayerMap.cpp
//  ThunderWarriors
//
//  Created by mac on 13-6-18.
//
//

#include "YHGameLayerMap.h"
#include "YHFileManager.h"
#include "cocos2d.h"
USING_NS_CC;

const int kMapColumns = 3;
const int kMapRows = 3;
const int kBaseWidth = 320;
const int kBaseHeight = 576;

class CCPointObject : CCObject
{
    CC_SYNTHESIZE(CCPoint, m_tRatio, Ratio)
    CC_SYNTHESIZE(CCPoint, m_tOffset, Offset)
    CC_SYNTHESIZE(CCNode *,m_pChild, Child)    // weak ref
    
    static CCPointObject * pointWithCCPoint(CCPoint ratio, CCPoint offset)
    {
        CCPointObject *pRet = new CCPointObject();
        pRet->initWithCCPoint(ratio, offset);
        pRet->autorelease();
        return pRet;
    }
    bool initWithCCPoint(CCPoint ratio, CCPoint offset)
    {
        m_tRatio = ratio;
        m_tOffset = offset;
        m_pChild = NULL;
        return true;
    }
};

IMPLEMENT_CREATE_CLASS(YHGameLayerMap)

YHGameLayerMap::YHGameLayerMap()
{
    m_BeginPos = CCPointZero;
    m_EndPos = CCPointZero;
    m_autoScrollCanvas = NULL;
    m_iRenderIndex = 0;
    m_isMapScrolling = false;

}

YHGameLayerMap::~ YHGameLayerMap()
{
    m_tiledLandMap->release();
    m_autoScrollCanvas->release();
}

void YHGameLayerMap::beginScroll(float speed)
{
    if (! m_isMapScrolling) {
        m_autoScrollCanvas->beginAutoScroll(speed);
        m_isMapScrolling = true;
    }
    /*CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    float duration = winSize.height / speed ;
    
    CCMoveBy* moveBy = CCMoveBy::create(duration, CCPointMake(0, -winSize.height * 4));
    CCCallFunc* callFunc = CCCallFunc::create(this, callfunc_selector(YHGameLayerMap::resetPos));
    
    CCFiniteTimeAction* sequence = CCSequence::create(moveBy, callFunc, NULL);
    m_ViewNode->runAction(sequence);*/
}

void YHGameLayerMap::resetPos()
{
   /* m_ViewNode->setPosition(CCPointMake(0, 0));
    
    ccArray* children = m_ViewNode->getParallaxArray();
    for (int i = 0; i < children->num; i ++) {
        CCObject* obj = children->arr[i];
        CCPointObject* pointObject = (CCPointObject*)(obj);
        if (pointObject) {
            pointObject->setOffset(ccp(-320,480));
        }
    }
    
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    float duration = winSize.height / 40 ;
    
    CCMoveBy* moveBy = CCMoveBy::create(duration, CCPointMake(0, -winSize.height * 4));
    m_ViewNode->runAction(moveBy);*/
}





bool YHGameLayerMap::init()
{
    //this->setTouchEnabled(true);
    loadMap();
    return true;
}


void YHGameLayerMap::visit()
{
    YHLayer::visit();
}

bool YHGameLayerMap::loadMap()
{
    const char* mapLandName = "sky_map.tmx";
    const char* mapCloudName = "cloudMap.tmx";
    m_tiledLandMap = CCTMXTiledMap::create(mapLandName);
    m_tiledCloudMap = CCTMXTiledMap::create(mapCloudName);
    m_tiledLandMap->retain();
    m_tiledCloudMap->retain();
    
    m_tiledLandMap->ignoreAnchorPointForPosition(false);
    m_tiledLandMap->setAnchorPoint(ccp(0, 0));
    m_tiledLandMap->setPosition(ccp(0,0));
    CCSize mapSize = m_tiledLandMap->getContentSize();
    
    CCObject* pObj = NULL;
    CCARRAY_FOREACH(m_tiledLandMap->getChildren(), pObj)
    {
        CCSpriteBatchNode* child = (CCSpriteBatchNode*)pObj;
        child->getTexture()->setAntiAliasTexParameters();
    }
    
    CCARRAY_FOREACH(m_tiledCloudMap->getChildren(), pObj)
    {
        CCSpriteBatchNode* child = (CCSpriteBatchNode*)pObj;
        child->getTexture()->setAntiAliasTexParameters();
    }
    
    m_autoScrollCanvas = YHAutoScrollCanvas::createWithSize(CCSizeMake(kBaseWidth, kBaseHeight));
    m_autoScrollCanvas->setDelegate(this);
    //m_autoScrollCanvas->beginAutoScroll(40);
    this->addChild(m_autoScrollCanvas);
    
    /*CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    m_tiledCloudMap->setScale(2.1f);
    m_ViewNode = CCParallaxNode::create();
    this->addChild(m_ViewNode);
    beginScroll(40);*/
    
    return  true;
}

void YHGameLayerMap::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
    
   /* CCTouch* touch = (CCTouch*) (pTouches->anyObject());
    m_pTouch = touch;
    CCPoint touchPoint = touch->getLocation();
    m_BeginPos = this->convertToNodeSpace(touchPoint); */
}

void YHGameLayerMap::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)
{
   /* CCTouch* touch = (CCTouch*) (pTouches->anyObject());
    if (m_pTouch != touch) {
        CCLOG("Return");
        return;
    }
    CCPoint touchPoint = touch->getLocation();
    m_EndPos = this->convertToNodeSpace(touchPoint);
    
    if (ccpDistance(m_BeginPos, m_EndPos) > 3) {
        CCPoint deltaPoint = ccpSub(m_EndPos, m_BeginPos);
        CCPoint curPos = m_ViewNode->getPosition();
        CCPoint target = ccpAdd(curPos, deltaPoint);
        m_ViewNode->setPosition(target);
        m_BeginPos = m_EndPos;
    }
    else
    {
        //CCLOG("Shake");
    } */
}

void YHGameLayerMap::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
{
    
}

void YHGameLayerMap::ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent)
{
    
}

void YHGameLayerMap::RenderCanvas(CCPoint pos)
{
    if (m_iRenderIndex == 9) {
        m_iRenderIndex = 0;
    }
    //pos是将要渲染的地方的中点
    int x = m_iRenderIndex % kMapRows;
    int y = m_iRenderIndex / kMapRows;
    
    if (y < kMapColumns) {
        float xPos = - y * kBaseWidth;
        float yPos = - x * kBaseHeight;
        m_tiledLandMap->setPosition(ccp(xPos, yPos));
        CCLog("pos: %f, %f", xPos, yPos);
        m_tiledLandMap->visit();
        m_iRenderIndex ++ ;
    }
}

void YHGameLayerMap::pauseScroll(){
    if (m_isMapScrolling) {
        m_autoScrollCanvas->pauseSchedulerAndActions();
        m_isMapScrolling = false;
    }
}

void YHGameLayerMap::resumeScroll(){
    if (m_autoScrollCanvas != NULL && !m_isMapScrolling) {
        m_autoScrollCanvas->resumeSchedulerAndActions();
    }
}